|
1
|
|
|
import React from 'react'; |
|
2
|
|
|
import { useHistory } from 'react-router-dom'; |
|
3
|
|
|
import ReportQuestion from './ReportQuestion.js'; |
|
4
|
|
|
|
|
5
|
|
|
class ReportEdit extends React.Component { |
|
6
|
|
|
constructor(props) { |
|
7
|
|
|
super(props); |
|
8
|
|
|
this.state = { |
|
9
|
|
|
kmom: this.props.match.params.kmom, |
|
10
|
|
|
reportData: [], |
|
11
|
|
|
editFields: [], |
|
12
|
|
|
}; |
|
13
|
|
|
this.getReport(); |
|
14
|
|
|
|
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
getReport = () => { |
|
18
|
|
|
fetch("http://localhost:8333/reports/" + this.state.kmom, { |
|
19
|
|
|
method: "GET", |
|
20
|
|
|
headers: { |
|
21
|
|
|
'Accept': 'application/json', |
|
22
|
|
|
'Content-Type': 'application/json', |
|
23
|
|
|
}, |
|
24
|
|
|
}).then((res) => { |
|
25
|
|
|
return res.json(); |
|
26
|
|
|
}).then((res) => { |
|
27
|
|
|
if(res.data){ |
|
28
|
|
|
var parsed = JSON.parse(res.data.text); |
|
29
|
|
|
var data = parsed.map((item, index) => { |
|
30
|
|
|
item['id'] = index; |
|
31
|
|
|
return item; |
|
32
|
|
|
}); |
|
33
|
|
|
|
|
34
|
|
|
this.setState({reportData: data}); |
|
35
|
|
|
this.setState({editFields: this.createReportQuestion(data)}) |
|
36
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
}); |
|
40
|
|
|
}; |
|
41
|
|
|
postReport = (data) => { |
|
42
|
|
|
|
|
43
|
|
|
fetch("http://localhost:8333/reports", { |
|
44
|
|
|
method: "POST", |
|
45
|
|
|
headers: { |
|
46
|
|
|
'Accept': 'application/json', |
|
47
|
|
|
'Content-Type': 'application/json', |
|
48
|
|
|
}, |
|
49
|
|
|
body: JSON.stringify({ |
|
50
|
|
|
kmom: this.state.kmom, |
|
51
|
|
|
data: data, |
|
52
|
|
|
token: localStorage.getItem('jwt') |
|
53
|
|
|
}) |
|
54
|
|
|
}).then((res) => { |
|
55
|
|
|
return (res.json()); |
|
56
|
|
|
|
|
57
|
|
|
}).then((res) => { |
|
58
|
|
|
console.log(res); |
|
59
|
|
|
if(res.data.status === 'ok'){ |
|
60
|
|
|
var url = '/reports/week/' + this.state.kmom; |
|
61
|
|
|
window.location.replace(url); |
|
62
|
|
|
} |
|
63
|
|
|
}) |
|
64
|
|
|
var url = '/reports/week/' + this.state.kmom; |
|
65
|
|
|
window.location.replace(url); |
|
66
|
|
|
}; |
|
67
|
|
|
|
|
68
|
|
|
createReportQuestion = (data) => { |
|
69
|
|
|
var questions = data.map((item, index) => { |
|
70
|
|
|
return ( |
|
71
|
|
|
<ReportQuestion |
|
72
|
|
|
key={index} |
|
73
|
|
|
index={item.id} |
|
74
|
|
|
question={item.question} |
|
75
|
|
|
answer={item.answer} |
|
76
|
|
|
triggerQuestion={this.handleOnChange} |
|
77
|
|
|
triggerAnswer={this.handleOnChange} |
|
78
|
|
|
/> |
|
79
|
|
|
); |
|
80
|
|
|
}); |
|
81
|
|
|
var newData = this.state.editFields; |
|
82
|
|
|
newData.push(questions); |
|
83
|
|
|
this.setState({editFields: newData}); |
|
84
|
|
|
}; |
|
85
|
|
|
addQuestion = (event) => { |
|
86
|
|
|
if (event) { |
|
87
|
|
|
event.preventDefault(); |
|
88
|
|
|
} |
|
89
|
|
|
var item = {id:(this.state.reportData.length), question: "New", answer: "New"}; |
|
90
|
|
|
this.createReportQuestion([item]); |
|
91
|
|
|
var newState = this.state.reportData; |
|
92
|
|
|
newState.push(item); |
|
93
|
|
|
this.setState({reportData: newState}); |
|
94
|
|
|
}; |
|
95
|
|
|
|
|
96
|
|
|
handleSubmit = (event) => { |
|
97
|
|
|
event.preventDefault(); |
|
98
|
|
|
this.postReport(JSON.stringify(this.state.reportData)); |
|
99
|
|
|
}; |
|
100
|
|
|
|
|
101
|
|
|
handleOnChange = (event) => { |
|
102
|
|
|
|
|
103
|
|
|
let nam = event.target.name; |
|
104
|
|
|
let val = event.target.value; |
|
105
|
|
|
let id = event.target.id; |
|
106
|
|
|
var state = this.state.reportData; |
|
107
|
|
|
var newState = []; |
|
108
|
|
|
|
|
109
|
|
|
state.forEach((item)=>{ |
|
110
|
|
|
|
|
111
|
|
|
if(item.id == id){ |
|
112
|
|
|
item[nam] = val; |
|
113
|
|
|
} |
|
114
|
|
|
newState.push(item); |
|
115
|
|
|
|
|
116
|
|
|
}); |
|
117
|
|
|
|
|
118
|
|
|
this.setState({reportData: newState}); |
|
119
|
|
|
}; |
|
120
|
|
|
|
|
121
|
|
|
|
|
122
|
|
|
render() { |
|
123
|
|
|
return ( |
|
124
|
|
|
<div className="edit_report_container"> |
|
125
|
|
|
|
|
126
|
|
|
<form className="edit-question-form" onSubmit={this.handleSubmit}> |
|
127
|
|
|
<div> |
|
128
|
|
|
{this.state.editFields} |
|
129
|
|
|
</div> |
|
130
|
|
|
<button onClick={this.addQuestion}>Add question</button> |
|
131
|
|
|
<input type="submit" value="Spara"/> |
|
132
|
|
|
</form> |
|
133
|
|
|
|
|
134
|
|
|
</div> |
|
135
|
|
|
|
|
136
|
|
|
) |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
export default ReportEdit; |
|
142
|
|
|
|
|
143
|
|
|
|